﻿using System;
using System.Web;
using HNAlbum.Helper;

namespace HNAlbum.Class
{
    /// <summary>
    /// The module only allows browsing for APSX pages
    /// </summary>
    public class BrowsingModule : IHttpModule
    {
        // In the Init function, register for HttpApplication 
        // events by adding your handlers.
        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(this.Application_BeginRequest);
        }

        private void Application_BeginRequest(Object source, EventArgs e)
        {
            // Create HttpContext objects to access request and response properties.
            HttpContext context = ((HttpApplication)source).Context;

            //File extension
            string fileExtension = VirtualPathUtility.GetExtension(context.Request.FilePath);
            //Browsing for any files other than aspx -> throw page not found exception
            if (fileExtension.Equals(".xml") || fileExtension.Equals("hna"))
            {
                throw new HttpException(404, Constant.ExceptionMessage.Page.PageNotFound);
            }
        }

        public void Dispose() { }
    }
}
